﻿using Blog.Core.Domain.Dictionary;
using Blog.Core.DTO;
using Blog.Core.DTO.ViewModel;
using Blog.Core.DTO.ViewModel.Dictionary;
using Blog.Core.IRepositories;
using Blog.Core.IRepositories.Dictionary;
using Blog.Core.IServices.Dictionary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Blog.Core.Services.Dictionary
{
    /// <summary>
    /// 字典类型表的服务层
    /// </summary>
    public class DictTypeService : BaseService<DictType>, IDictTypeService
    {
        private readonly IDictTypeRepository dictTypeRepository;
        private readonly IUnOfWork unOfWork;
        public DictTypeService(IRepository<DictType> repository,IDictTypeRepository dictTypeRepository, IUnOfWork unOfWork) : base(repository, unOfWork)
        {
            this.dictTypeRepository = dictTypeRepository;
            this.unOfWork = unOfWork;
        }
        /// <summary>
        /// 字典类型列表
        /// </summary>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public async Task<ResponseResultDTO<PageDTO<DictType>>> GetDictType(int pageindex,int pagesize,string name)
        {
            var list = from x in await dictTypeRepository.GetAll()
                       select x;
            if(!string.IsNullOrEmpty(name))
            {
                list = list.Where(m => m.Dict_type.Contains(name));
            }
            PageDTO<DictType> pageDTO = new PageDTO<DictType>() { 
                RecordCount=list.Count(),
                Plist=list.OrderBy(m=>m.Dict_id).Skip((pageindex-1)*pagesize).Take(pagesize).ToList(),
            };
            return new ResponseResultDTO<PageDTO<DictType>>
            {
                Code = ResultStatusCode.SUCCESS,
                Data = pageDTO,
                Message = "查询成功！",
            };
        }
        /// <summary>
        /// 字典类型录入
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<ResponseResultDTO<int>> PostDictType(PostDictTypeDTO dto)
        {
            if (dto == null)
            {
                return new ResponseResultDTO<int>
                {
                    Code = ResultStatusCode.SUCCESS,
                    Message = "参数错误！",
                };
            }
            DictType entity = new DictType()
            {
                Dict_id = dto.Dict_id,
                CreateName = dto.UserName,
                CreateTime = DateTime.Now,
                Delete = dto.Delete,
                Dict_type = dto.Dict_type,
                remark = dto.remark,
                Status = dto.Status,
                UpdateName = dto.UserName,
                UpdateTime = DateTime.Now,
            };
            await dictTypeRepository.Add(entity);
            await unOfWork.SaveChanges();
            return new ResponseResultDTO<int>
            {
                Code = ResultStatusCode.SUCCESS,
                Message = "录入成功！"
            };
        }
        /// <summary>
        /// 字典类型修改
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<ResponseResultDTO<int>> PutDictType(PostDictTypeDTO dto)
        {
            if (dto == null)
            {
                return new ResponseResultDTO<int>
                {
                    Code = ResultStatusCode.SUCCESS,
                    Message = "参数错误！",
                };
            }
            DictType entity = new DictType()
            {
                Dict_id = dto.Dict_id,
                CreateName = dto.CreateName,
                CreateTime = dto.CreateTime,
                Delete = dto.Delete,
                Dict_type = dto.Dict_type,
                remark = dto.remark,
                Status = dto.Status,
                UpdateName = dto.UserName,
                UpdateTime = DateTime.Now,
            };
            await dictTypeRepository.Update(entity);
            await unOfWork.SaveChanges();
            return new ResponseResultDTO<int>
            {
                Code = ResultStatusCode.SUCCESS,
                Message = "修改成功！"
            };
        }
    }
}
